Using Ethers.js
Ethers.js is a compact library for interacting with Ethereum Virtual Machine (EVM) based blockchains. With Gnosis being an EVM chain, you can use Ethers.js to interact with the Gnosis ecosystem.
Adding Ethers.js to your Project
- yarn
- npm
yarn add ethers
npm install --save ethers
To import the ethers library into your project using Node.js, use the following:
const { ethers } = require("ethers");
import { ethers } from "ethers";
Connecting to Gnosis with MetaMask
After installing, you need to create a web3 instance and set a provider. Most Ethereum supported wallets, such as MetaMask, have an EIP-1193 compliant provider at window.ethereum
. This works for connecting to Gnosis as well.
// A Web3Provider wraps a standard Web3 provider, which is
// what MetaMask injects as window.ethereum into each page
const provider = new ethers.providers.Web3Provider(window.ethereum)
// MetaMask requires requesting permission to connect users accounts
await provider.send("eth_requestAccounts", []);
// The MetaMask plugin also allows signing transactions to
// send ether and pay to change state within the blockchain.
// For this, you need the account signer...
const signer = provider.getSigner()
View the official Ethers.js MetaMask docs here.
Connecting to Gnosis via RPC
// If you don't specify a //url//, Ethers connects to the default
// (i.e. ``http:/\/localhost:8545``)
const provider = new ethers.providers.JsonRpcProvider();
// The provider also allows signing transactions to
// send ether and pay to change state within the blockchain.
// For this, we need the account signer...
const signer = provider.getSigner()
View the official Ethers.js RPC docs here.
Interacting with a Contract
To connect to and interact with a deployed contract, you can do the following:
// The Contract object
const Contract = new ethers.Contract(Address, Abi, provider);
View the official Ethers.js contract docs here.